﻿// Copyright (c) 2014 Bryan C. O'Connell
// http://wsubi.codeplex.com/license
// Purpose: Informant displays information to a user.

using System;
using System.IO;
using System.Collections.Generic;

namespace Wsubi.Code.Utilities
{
    static class Informant
    {
        /// <summary>Displays information about an Error to the console.</summary>
        /// <param name="Info">Information about where the error came from or what was happenning.</param>
        /// <param name="Error">Instance of the Exception that occured.</param>
        public static void DisplayError(string Info, Exception Error)
        {
            Console.WriteLine("\n" + Info);
            Console.WriteLine("Message: " + Error.Message);
        }

        /// <summary>Nicely displays a list of strings to the console.</summary>
        /// <param name="Results">The list of items that needs to be formatted and displayed.</param>
        public static void DisplayFormattedResults(List<string> Results)
        {
            int CharCount = 0;
            const string PAD = "   "; // 3 spaces

            Console.WriteLine("\n");

            foreach (string Record in Results)
            {
                if (CharCount + Record.Length + PAD.Length < Console.WindowWidth)
                {
                    Console.Write(Record + PAD);
                    CharCount += Record.Length + PAD.Length;
                }
                else
                {
                    Console.Write("\n\n" + Record + PAD);
                    CharCount = Record.Length + PAD.Length;
                }
            }

            Console.WriteLine("\n\n");
        }

        /// <summary>Writes content to a file.</summary>
        /// <param name="Content">Content to write. Each List record will be its own line in the file.</param>
        /// <param name="OutputFilepath">Full path and name of the file to be created.</param>
        public static void WriteContentToFile(List<string> Content, string OutputFilepath)
        { 
            if (File.Exists(OutputFilepath))
            {
                File.Delete(OutputFilepath);
            }

            using (StreamWriter Writer = new StreamWriter(OutputFilepath))
            { 
                foreach(string Record in Content)
                {
                    Writer.WriteLine(Record);
                }
            }
        }

        /// <summary>Displays the requested help text to the user (if found).</summary>
        /// <param name="HelpItemName">Help item being requested.</param>
        public static void ShowHelp(string HelpItemName)
        {
            // TODO: Need to fix how help text is accessed.
            //Help.ResourceManager.IgnoreCase = true;
            //string HelpText = Help.ResourceManager.GetString(HelpItemName);
            string HelpText = "";

            if (String.IsNullOrEmpty(HelpText))
            {
                HelpText = "So sorry, no Help items correspond to your request.";
            }

            // /Replace return characters; they mess up the output formatting.
            HelpText = HelpText.Replace("\r", "\n");

            foreach (char c in HelpText)
            {
                Console.Write(c);
            }

            Console.WriteLine("\n");
        }
    }
}
